/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hbPackage;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author windySwing
 */
public class registerPaymentSvl extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            DatabaseAccess da = new DatabaseAccess();
            HttpSession session = request.getSession();
            ArrayList arrOrderDetail = (ArrayList) session.getAttribute("orderDetailList");
            int _orderID = Integer.parseInt(session.getAttribute("orderID").toString());
            String decision = request.getParameter("btn_submit");
            orderClass _order = da.getOrder(_orderID);
            SimpleDateFormat sf = new SimpleDateFormat("MM/dd/yyyy");

            if (decision.equalsIgnoreCase("Confirm")) {

                // update order status
                String[] _key = {"status"};
                ArrayList _arrOrder = new ArrayList();
                String _status = "Paid";
                _arrOrder.add(_status);
                String[] _condition = {("orderID=" + Integer.toString(_orderID))};
                da.Update("[Order]", _key, _arrOrder, _condition);

                

                //add new entity into payment table
                ArrayList _arrPayment = new ArrayList();
                _arrPayment.add(_orderID);
                _arrPayment.add(_order.getTotalPayment());
                _arrPayment.add(_order.getStore_ID());
                _arrPayment.add(sf.format(_order.getDate()));

                da.Insert("Payment", _arrPayment);
            } else if (decision.equalsIgnoreCase("Cancel")) {
                //delete orderDetails
                String[] _orderDetailCondidtion = {("order_ID=" + Integer.toString(_orderID))};
                da.Delete("OrderDetail", _orderDetailCondidtion);

                //delete order
                String[] _orderCondition = {("orderID=" + Integer.toString(_orderID))};
                da.Delete("[Order]", _orderCondition);

                //update item quantity in warehouse
                for (int i = 0; i < arrOrderDetail.size(); i++) {
                    orderDetailClass _orderDetail = (orderDetailClass) arrOrderDetail.get(i);

                    wareHouseClass _wareHouse = da.getWareHouse(_order.getStore_ID(), _orderDetail.getItem_ID());
                    int tempQuantity = _wareHouse.getQuantity();
                    int newQuantiy = tempQuantity + _orderDetail.getQuantity();
                    String[] _key = {"Quantity"};
                    ArrayList arr = new ArrayList();
                    arr.add(newQuantiy);
                    String[] _condition = {("store_ID=" + Integer.toString(_order.getStore_ID()))
                            , ("item_ID=" + Integer.toString(_orderDetail.getItem_ID()))};
                    da.Update("WareHouse", _key, arr, _condition);
                }

            }

            session.removeAttribute("orderDetailList");
            session.removeAttribute("orderID");

            out.println("<script>window.location=\"admin/recordOrder.jsp\";</script>");

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
